// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async (event, context) => {
	const wxContext = cloud.getWXContext()
	openid = wxContext.OPENID
	let num = 3 //获取数量限制3
	let myself = await db.collection("users").where({ _openid: openid }).get()
	myself = myself.data[0]
	let data = []
	let data2 = []
	if(myself == undefined){
		data = await db.collection('match').limit(num).get()
		data = data.data
	}else{
		data = await db.collection('match').where({
			area: myself.school
		}).limit(num).get()
		if (data.data.length < num) {
			data2 = await db.collection('match').limit(num - data.data.length).get()
		}
		data = data.data.concat(data2.data)
	}
	return data
}